using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;

namespace blogapi
{
    public partial class kategorie : Form
    {
        public kategorie(nastaveni nas, TextBox textboox,pages ser)
        {
            InitializeComponent();
            nast = nas;
            serv = ser;
            textbox = textboox;
            vlakno = null;
            checking = false;
        }
        nastaveni nast;
        pages serv;
        TextBox textbox;
        Thread vlakno;

        public void setlang()
        {
            menuItem1.Text = nast.getlang("moznosti");
            menuItem3.Text = nast.getlang("zpet");
            menuItem4.Text = nast.getlang("aktualizovat");
            menuItem2.Text = nast.getlang("zvolit");
            label1.Text = nast.getlang("volbakat");
            button1.Text = nast.getlang("storno");
            label2.Text = nast.getlang("procestrvat");
            Text = "W - "+nast.getlang("kategorie");
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            stahuj(true);
        }

        public void stahuj(bool vynucenej)
        {
            bool doitnow = false;
            if (vynucenej || nast.autokatdown)
            {
                if (nast.dotazovatse)
                {
                    if (DialogResult.OK == MessageBox.Show(nast.getlang("upozornenipripojeni"), nast.getlang("upozorneni"), MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1))
                    {
                        doitnow = true;
                    }
                }
                else
                {
                    doitnow = true;
                }
            }
            if (doitnow)
            {
                //aktualizace
                label3.Text = nast.getlang("nastavovanikategorii");
                menuItem1.Enabled = false;
                menuItem2.Enabled = false;
                panel1.Visible = true;
                vlakno = new Thread(sthju);
                vlakno.Priority = ThreadPriority.Highest;
                vlakno.IsBackground = true;
                vlakno.Start();
            }
            vygeneruj();
        }



        private void vygeneruj()
        {
            treeView1.Nodes.Clear();
            for(int i=0;i<serv.mozn.Count;i++)
            {
                blogs bl = (blogs)serv.mozn[i];
                TreeNode nod = new TreeNode(bl.url);
                nod.Checked = false;
                nod.Tag = 0;
                for (int p = 0; p < bl.kategorie.Count; p++)
                {
                        TreeNode tr = new TreeNode(bl.kategorie[p].ToString());
                        tr.Checked = false;
                        tr.Tag = 1;
                        nod.Nodes.Add(tr);
                }
                treeView1.Nodes.Add(nod);
            }
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            textbox.Text = "";
            for (int i = 0; i < treeView1.Nodes.Count; i++)
            {
                TreeNode nod = treeView1.Nodes[i];
                for (int p = 0; p < nod.Nodes.Count; p++)
                {
                    if (nod.Nodes[p].Checked == false)
                    {
                        continue;
                    }
                    if (textbox.Text != "")
                    {
                        textbox.Text += ",";
                    }
                    textbox.Text += nod.Nodes[p].Text;
                }
            }
            this.Hide();
        }

        bool checking;
        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (checking==true)
            {
                return;
            }
                checking = true;
                if ((int)e.Node.Tag == 0)
                {
                    for (int i = 0; i < e.Node.Nodes.Count; i++)
                    {
                        e.Node.Nodes[i].Checked = e.Node.Checked;
                    }
                }
                else
                {
                    bool stav = true;
                    TreeNode nod = e.Node.Parent;
                    for (int i = 0; i < nod.Nodes.Count; i++)
                    {
                        stav = nod.Nodes[i].Checked;
                        if (stav == false)
                        {
                            break;
                        }
                    }
                    nod.Checked = stav;
                }
                checking = false;
        }


        private void publikujdolabelu(string text)
        {
            label3.Text = text;
        }
        delegate void publikujdolabeludel(string text);
        private void zobraz()
        {
            panel1.Visible = false;
            menuItem1.Enabled = true;
            menuItem2.Enabled = true;
            vygeneruj();
        }
        delegate void zobrazdel();
        private void sthju()
        {
            ApiHandler.PostHandler post = new ApiHandler.PostHandler();
            for (int i = 0; i < serv.mozn.Count; i++)
            {
                blogs bl = (blogs)serv.mozn[i];
                label3.Invoke(new publikujdolabeludel(publikujdolabelu), new object[] { nast.getlang("stahovanikat")+" " + bl.url });
                bl.kategorie.Clear();
                xmlrpcCF.Category[] cats = null;
                string uziv = bl.user;
                string pass = bl.pass;
                if (bl.pass == "" || bl.user == "")
                {
                    blogs blg = new blogs();
                    blg.url = bl.url;
                    prihlasovaciform prihlas = new prihlasovaciform(blg, nast);
                    prihlas.ShowDialog();
                    uziv = blg.user;
                    pass = blg.pass;
                    if (prihlas.checkBox1.Checked == true)
                    {
                        bl.user = blg.user;
                        bl.pass = blg.pass;
                    }
                    prihlas = null;
                }
                try
                {
                    cats = post.GetCategories(uziv, pass, bl.url);
                }
                catch
                {
                    continue;
                }
                for (int p = 0; p < cats.Length; p++)
                {
                    if (cats[p].title != null)
                    {
                        bl.kategorie.Add(cats[p].title);
                    }
                    else
                    {
                        bl.kategorie.Add(cats[p].description);
                    }
                }
            }
            panel1.Invoke(new zobrazdel(zobraz), new object[] { });
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (vlakno != null)
            {
                vlakno.Abort();
                vlakno = null;
            }
            zobraz();
        }
    }
}